import 'package:FlutterReader/l10n/LanguageProvider.dart';
import 'package:FlutterReader/reading/book_provider.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:FlutterReader/router/MyRouter.dart';
import 'package:FlutterReader/theme/dark_theme.dart';
import 'package:FlutterReader/theme/light_theme.dart';
import 'package:FlutterReader/theme/theme_provider.dart';
import '/page/face_page.dart';
import 'package:provider/provider.dart';

import 'generated/l10n.dart';

Future<void> main() async {
  // ------------------------------------------------------------------ //
  WidgetsFlutterBinding.ensureInitialized();

  // 沉浸式状态
  SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
    statusBarColor: Colors.transparent,
    statusBarBrightness: Brightness.dark,
  ));

  runApp(MyApp());
}


class MyApp extends StatefulWidget {

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
        providers: [
          // 构造函数完成初始化
          ChangeNotifierProvider(create: (_) => ThemeProvider()),
          ChangeNotifierProvider(create: (_) => LanguageProvider()),
          ChangeNotifierProvider(create: (_) => BookProvider()),
        ],
      child: Consumer3<ThemeProvider,LanguageProvider,BookProvider>(
        builder: (context, themeProvider, languageProvider, bookProvider , child) {
          return MaterialApp(
            // 国际化
            // 多语言国际化配置
            locale: languageProvider.currentLocale,
            supportedLocales: S.delegate.supportedLocales,
            localizationsDelegates: [
              S.delegate,
              GlobalMaterialLocalizations.delegate,
              GlobalWidgetsLocalizations.delegate,
              GlobalCupertinoLocalizations.delegate,
            ],


            // 路由
            // 使用这种方式可以指定路由切换的动画过度效果
            onGenerateRoute: MyRouter.onGenerateRoute,

            title: "Reading",

            themeMode: themeProvider.themeMode, // 从Provider获取
            theme: lightTheme,        // 亮色主题
            darkTheme: darkTheme,     // 暗色主题
            home: const BottomMenuBarPage(),

            debugShowMaterialGrid: false,
            debugShowCheckedModeBanner: false,
          );
        },
      )
    );
  }

  @override
  void dispose() {
    super.dispose();
  }
}
